' quickhelp.bmx

Strict

Import brl.stream
Import brl.filesystem

Type TToken
	Field token$
	Field help$
	Field ref$	
	Method Create:TToken(t$,h$,r$)
		token=t
		help=h
		ref=r
		Return Self
	End Method
End Type

Type TExactQuickHelp Extends TQuickHelp	'case sensitive for c/java usage
		
	Method AddCommand:TQuickHelp(t$,l$,a$)
		map.Insert t,New TToken.Create(t$,l$,a$)
	End Method
	
	Method Token$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd))
		If t Return t.token
	End Method
	
	Method Help$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd))
		If t Return t.help
	End Method
	
	Method Link$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd))
		If t Return t.ref
	End Method
End Type

Type TQuickHelp
	Field map:TMap=New TMap	'key=lower(token) value=token:TToken
		
	Method AddCommand:TQuickHelp(t$,l$,a$)
		map.Insert t.toLower(),New TToken.Create(t$,l$,a$)
	End Method
	
	Method Token$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd.toLower()))
		If t Return t.token
	End Method
	
	Method Help$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd.toLower()))
		If t Return t.help
	End Method
	
	Method Link$(cmd$)
		Local t:TToken
		t=TToken(map.ValueForKey(cmd.toLower()))
		If t Return t.ref
	End Method
	
	Function LoadCommandsTxt:TQuickHelp(bmxpath$)
		Local	stream:TStream
		Local	qh:TQuickHelp
		Local	l$,c$,p,q
		Local	token$,help$,anchor$
		
		stream=ReadFile(bmxpath+"/doc/bmxmods/commands.txt")
		If stream=Null Return Null
		qh=New TQuickHelp
		While Not Eof(stream)
			l$=ReadLine(stream)
			p=Len(l)
			q=l.find("|")
			If q<>-1 And q<p p=q
			q=l.find("(")
			If q<>-1 And q<p p=q
			q=l.find("$")
			If q<>-1 And q<p p=q
			q=l.find("#")
			If q<>-1 And q<p p=q
			q=l.find("!")
			If q<>-1 And q<p p=q
			q=l.find("%")
			If q<>-1 And q<p p=q
			q=l.find(":")
			If q<>-1 And q<p p=q
			token$=l$[..p]
			help$=""
			anchor$=""
			q=l.find("|")
			If q
				help=l[..q]
				anchor=l[q+1..]
			EndIf			
			qh.AddCommand token,help,anchor
		Wend
		stream.Close
		Return qh
	End Function
End Type
